var tenementFlag = "owner";
var tipName = "业主";
var individual_isAllowLoad = true;
var validate_pass = true;
/**
 * 个人住户
 * @param {Object} windowObj  窗体操作对象
 * @param {Object} fromId  个人住户表单ID
 */
function Individual(windowObj,fromId){

	this.windowObj = windowObj;

	this.formId = fromId;   //当前个人住户表单ID
	this.formObj = document.getElementById(fromId); //当前个人住户表单

	var oldCertificateNo = null;
	var oldCertificateType = null;

	/**
	 * 初始化方法
	 */
	this.init = function(){
		setCertificate(); //设置证件类型、证件号码的值
	}

	/**
	 * 通过证件获取、并设置个人住户信息
	 */
	this.setInfoByCertificate = function(){
		var certificateNo = document.forms(this.formId).certificateNo.value;
		var certificateType = document.forms(this.formId).certificateType.value;
		individual_isAllowLoad = true;
		validate_pass = true;
		if(checkAllowSubmit(certificateNo,certificateType)){
			//判断当前证件个人信息能否作为业主或者租户(加入该个人信息已为家庭成员,则不能再添加为业主)
			isAllowLoad(certificateNo,certificateType);
		}
	}

	/**********私有方法***************/
	function checkAllowSubmit(certificateNo,certificateType){
		//证件号码为空 不到后台拿数据
		if(certificateNo == null || certificateNo == "") return false;
		//证件类型、号码同上次的相同，不到后台拿数据
		if(certificateNo == oldCertificateNo && certificateType == oldCertificateType) return false;
		return true;
	}
	/**
	 * 验证当前证件信息能否作为业主、租户添加
	 * @param {Object} certificateNo  证件号码
	 * @param {Object} certificateType 证件类型
	 */
	function isAllowLoad(certificateNo,certificateType){
		var url = PROJECT_NAME_PATH_MYCE + "/customer_ownerAction.do?method=validateOwnerAllowLoad";
		var lesseeUrl = PROJECT_NAME_PATH_MYCE + "/customer_lesseeAction.do?method=validateLesseeAllowLoad";
		url = tenementFlag =="lessee" ?lesseeUrl : url;
		var parameters = "certificateType=" + certificateType + "&certificateNo=" + certificateNo;
			parameters += "&participantId=" + document.forms(this.formId).participantId.value;
			parameters += "&oldParticipantId=" + document.forms(this.formId).oldParticipantId.value;
			parameters += "&index=" + document.forms(this.formId).index.value;
			parameters += "&optionType=" + document.forms(this.formId).optionType.value;//操作类型

		sendAjaxRequest(url,parameters,function(result){
			var obj = result.responseText;
			setTip();
			//可以导入
			if(obj == 'true'){
				//TODO 成功验证号码
				getIndividualInfo(certificateNo,certificateType);
			}else if(obj == 'owner'){
				validate_pass = false;
				individual_isAllowLoad = false;
				Alert("该用户已经登记为业主了，请重新输入!");
				setCertificateNull();
			}
			//已经添加过,存在业主列表中了
			else if(obj == 'existCache'){
				validate_pass = false;
				individual_isAllowLoad = false;
				Alert("该"+tipName+"已经存在列表中了,请重新输入");
				setCertificateNull();
			}//该住户已经是租户,不能导入
			else if(obj == 'lessee'){
				validate_pass = false;
				individual_isAllowLoad = false;
				Alert("该住户已经是租户,请重新输入");
				setCertificateNull();
			}//该住户已经是家庭成员了，不可导入
			else if(obj == 'member'){
				validate_pass = false;
				individual_isAllowLoad = false;
				Alert("该住户已经是家庭成员,请重新输入");
				setCertificateNull();
			}
		});
	}
	/**
	 * 从后台获取个人信息
	 * @param {Object} certificateNo
	 * @param {Object} certificateType
	 */
	function getIndividualInfo(certificateNo,certificateType){
		var url = PROJECT_NAME_PATH_MYCE + "/customer_ownerAction.do?method=queryParticipant";
		var parameter = "certificateType=" + certificateType + "&certificateNo=" + certificateNo;
		sendAjaxRequest(url,parameter,setIndividualInfo);
	}
	/**
	 * 根据提供的个人信息，设置表单信息
	 * @param {Object} IndividualInfo
	 */
	function setIndividualInfo(individualInfo){
		if(obj == 'false') return;

		if(individualInfo.responseText == 'false') return;

		var flag = confimImport();
		if(!flag) return;
		var obj=eval('(' + individualInfo.responseText + ')');
		setCertificate(); //重新初始化证件类型、证件号码
		document.forms(this.formId).participantId.value = obj.participantId //参与人ID

		document.forms(this.formId).participantName.value = obj.participantName; //参与人姓名
		document.forms(this.formId).appellative.value = obj.appellative; //参与人称呼
		document.forms(this.formId).telephone.value = obj.telephone; //参与人电话
		setSexValue(document.forms(this.formId).sex,obj.sex);

		with(document.forms(this.formId)){
			emergencyMan.value = obj.emergencyMan; //紧急联系人
			emergencyPhone.value = obj.emergencyPhone;//紧急联系电话
			birthDate.value = obj.birthDate; //出生日期
			email.value = obj.email; //电子邮件
			homeplace.value = obj.homeplace;//籍贯
			registerCar.value = obj.registerCar;// 车辆情况
			registerPlace.value = obj.registerPlace; //户口所在地
			profession.value = obj.profession; //职业
			corporation.value = obj.corporation;//单位名称
			linkAddress.value = obj.linkAddress; //联系地址
			remark.value = obj.remark; //备注
		}
		//处理兴趣爱好

		//将已选择中所有的选项移到左边
		var selectedOptions = $('selectedInterest').options;
		var length = selectedOptions.length;
		for(i = 0 ; i  < length; i++){
			$('unSelectInterest').appendChild(selectedOptions[0]);
		}

		var opt = $('unSelectInterest').options;//所有兴趣爱好
		var interestArray = new Array();
		for(i = 0 ; i < obj.interest.length; i++){
			for(j = 0; j < opt.length; j++){
				if(obj.interest[i] == opt[j].value){
					interestArray[interestArray.length] = opt[j].value;
					$('selectedInterest').appendChild(opt[j]);
					j--;
				}
			}
		}

		$("interest").value = interestArray.join(",");
	}

	function confimImport(){
		if(confirm("你确定要导入相关记录吗？")){
			return true;
		}
		setCertificateNull();
		return false;
	}

	/**
	 * 设置证件类型，证件号码的值
	 */
	function setCertificate(){
		oldCertificateNo = document.forms(this.formId).certificateNo.value;
		oldCertificateType = document.forms(this.formId).certificateType.value;
	}
	/**
	*将证件类型至空
	*/
	function setCertificateNull(){
		document.forms(this.formId).certificateNo.value = "";
		document.forms(this.formId).certificateType.selectedIndex = 0;
	}
}

/**
* 企业相关操作
*/
function Enterprise(formId){

	var formId = formId;
	var formObj = document.forms(formId);

	var oldEnterpriseName = null;
	var oldCertificateNo = null;
	var oldCertificateType = null;

	var oldConsignerCertificateNo = null;
	var oldConsignerCertificateType = null;

	this.init = function(){
		setInitInfo();
	}

	this.processEnterpriseName = function(enterpriseName){

		if(enterpriseName == null || enterpriseName == "") return;
		if(oldEnterpriseName == enterpriseName) return;
		oldEnterpriseName = enterpriseName;
		var url = PROJECT_NAME_PATH_MYCE + "/customer_ownerAction.do?method=validateOrganizationOwner";
		var pars = "participantName=" + enterpriseName;
		pars += "&participantId=" + document.forms(formId).participantId.value;
		pars += "&oldParticipantId="  + document.forms(formId).oldParticipantId.value;
		pars += "&optionType=" + document.forms(formId).optionType.value; //操作类型
		pars += "&index=" + document.forms(formId).index.value; //缓存索引值
		sendAjaxRequest(url,pars,callBackProcess);
	}
	/**
	 *  处理企业信息回调
	 */
	function callBackProcess(result){
		var obj = result.responseText;
		ajaxSubmitException(obj);
		if(obj == "false"){
			return;
		}else if(obj == "true"){
			var url = PROJECT_NAME_PATH_MYCE + "/customer_customerAction.do?method=queryOrganizationAllByName";
			var parm = "participantName=" + document.forms(formId).participantName.value;
			parm += "&index=" + document.forms(formId).index.value;
			sendAjaxRequest(url,parm,backfill);
			return;
		}else if(obj == "owner"){
			Alert("该企业已经登记为业主了，请重新输入!");
		}else if(obj == "lessee"){
			Alert("该企业已经是租户,请重新输入");
		}else if(obj == "member"){
			Alert("该企业已经是家庭成员,请重新输入");
		}else if(obj == "existCache"){
			Alert("该企业已经存在列表中了,请重新输入");
		}
		document.forms(formId).participantName.value = "";
	}
	/**
	*  根据企业名称填充 企业信息
	*/
	function backfill(result){
		if(result.responseText == 'false')return;

		if(!checkFirm(result))return;

		var obj=eval('(' + result.responseText + ')');

		//处理基本信息:当带出企业后，要求保存企业ID
		document.forms(formId).participantId.value = obj.participantId;
		document.forms(formId).index.value = obj.index;

		document.forms(formId).participantName.value = obj.participantName;
		document.forms(formId).organizationLinkMan.value = obj.organizationLinkMan;
		document.forms(formId).address.value = obj.address;
		document.forms(formId).telephone.value = obj.telephone;
		document.forms(formId).zip.value = obj.zip;

		//法人基本信息
		document.forms(formId).corporationId.value = obj.corporationId;
		document.forms(formId).organizationPersonName.value = obj.organizationPersonName;
		document.forms(formId).corporationBirthDate.value = obj.corporationBirthDate;
		document.forms(formId).appellative.value = obj.appellative;
		document.forms(formId).organizationPersonPhone.value = obj.organizationPersonPhone;
		setSexValue(document.forms(formId).sex,obj.sex);
		document.forms(formId).email.value = obj.email;
		setSelectValue(document.forms(formId).certificateType,obj.certificateType);
		document.forms(formId).linkAddress.value = obj.linkAddress;
		document.forms(formId).certificateNo.value = obj.certificateNo;
		document.forms(formId).registerPlace.value = obj.registerPlace;
		document.forms(formId).remark.innerText = obj.remark;
		// 委托代理人处理

		document.forms(formId).consignerId.value = obj.consignerId;
		document.forms(formId).consignerName.value = obj.consignerName;
		document.forms(formId).consignerPhone.value = obj.consignerPhone;
		setSexValue(document.forms(formId).consignerSex,obj.consignerSex);
		document.forms(formId).consignerEmail.value = obj.consignerEmail;
		setSelectValue(document.forms(formId).consignerCertificateType,obj.consignerCertificateType);
		document.forms(formId).consignerHomeplace.value = obj.consignerHomeplace;
		document.forms(formId).consignerCertificateNo.value = obj.consignerCertificateNo;
		document.forms(formId).consignerAddress.value = obj.consignerAddress;

		setInitInfo();
	}

	/**
	*  法人资料导入
	*/
	this.corporation = function(){
		var certificateNo = document.forms(formId).certificateNo.value;
		var certificateType = document.forms(formId).certificateType.value;

		if(certificateNo == null || certificateNo == "") return;
		if(oldCertificateNo == certificateNo && oldCertificateType == certificateType){
			return;
		}
		var url = PROJECT_NAME_PATH_MYCE + "/customer_customerAction.do?method=queryParticipant";
		var parm = "certificateType=" + certificateType + "&certificateNo=" + certificateNo;
		sendAjaxRequest(url,parm,setCorporation);
	}
	//填充法人基本资料
	function setCorporation(result){
		var obj = eval('(' + result.responseText + ')');
		if(!obj) return;

		if(!confirm('该法人基本资料已经存在,是否导入?')){
			formObj.certificateNo.value = "";
			formObj.certificateType.selectedIndex = 0;
			return false;
		}
		document.forms(formId).corporationId.value = obj.corporationId;
		document.forms(formId).organizationPersonName.value = obj.participantName;
		document.forms(formId).corporationBirthDate.value = obj.birthDate;
		document.forms(formId).appellative.value = obj.appellative;
		document.forms(formId).organizationPersonPhone.value = obj.telephone;
		setSexValue(document.forms(formId).sex,obj.sex);
		document.forms(formId).email.value = obj.email;
		setSelectValue(document.forms(formId).certificateType,obj.certificateType);
		document.forms(formId).linkAddress.value = obj.linkAddress;
		document.forms(formId).certificateNo.value = obj.certificateNo;
		document.forms(formId).registerPlace.value = obj.registerPlace;
		document.forms(formId).remark.innerText = obj.remark;
		setInitInfo();
	}

	/**
	* 委托代理人
	*/
	this.consigner = function(){
		var consignerCertificateNo = document.forms(formId).consignerCertificateNo.value;
		var consignerCertificateType = document.forms(formId).consignerCertificateType.value;

		if(consignerCertificateNo == null || consignerCertificateNo == "") return;
		if(oldConsignerCertificateNo == consignerCertificateNo && oldConsignerCertificateType == consignerCertificateType)
			return;

		var url = PROJECT_NAME_PATH_MYCE + "/customer_customerAction.do?method=queryParticipant";
		var parm = "certificateType=" + consignerCertificateType + "&certificateNo=" + consignerCertificateNo;
		sendAjaxRequest(url,parm,setConsigner);
	}
	//设置委托代理人的信息
	function setConsigner(result){
		var obj = eval('(' + result.responseText + ')');
		if(!obj) return;
		if(!confirm("该委托代理人已存在,是否导入")){
			document.forms(formId).consignerCertificateType.selectedIndex = 0;
			document.forms(formId).consignerCertificateNo.value = "";
			return;
		}
		document.forms(formId).consignerId.value = obj.consignerId;
		document.forms(formId).consignerName.value = obj.participantName;
		document.forms(formId).consignerPhone.value = obj.telephone;
		setSexValue(document.forms(formId).consignerSex,obj.sex);
		document.forms(formId).consignerEmail.value = obj.email;
		setSelectValue(document.forms(formId).consignerCertificateType,obj.certificateType);
		document.forms(formId).consignerHomeplace.value = obj.registerPlace;
		document.forms(formId).consignerCertificateNo.value = obj.certificateNo;
		document.forms(formId).consignerAddress.value = obj.linkAddress;

		setInitInfo();
	}

	/**********private method 检查导入************/
	function checkFirm(){
		if(confirm("该企业名称已经存在，是否导入")){
			return true;
		}
		document.forms(formId).participantName.value = "";
		return false;
	}
	//设置相关初始化值
	function setInitInfo(){
		//初始化企业名称
		oldEnterpriseName = document.forms(formId).participantName.value;
		//初始化法人证件类型、证件号码
		oldCertificateNo = document.forms(formId).certificateNo.value;
		oldCertificateType = document.forms(formId).certificateType.value;
		//初始化委托代理人证件类型、证件号码
		oldConsignerCertificateNo = document.forms(formId).consignerCertificateNo.value;
		oldConsignerCertificateType = document.forms(formId).consignerCertificateType.value;
	}
}

/**
* 房屋选择相关业务操作
*/
function House(houseNumId){

	var houseNumberObj = document.getElementById(houseNumId);

	var oldHouseNum = null;
	/**
	*  打开选择房屋页面
	* @param {String} parentFrameId  父窗体div ID
	*/
	this.openSelectHouse = function (parentFrameId){
		var ownerUrl = PROJECT_NAME_PATH_MYCE + "/customer_chooseSingleHouseAction.do?method=chooseBuildingMain";


		ownerUrl += parentFrameId == "undefined"? "" :  "&parentFrameId=" + parentFrameId;
		top.windowOpen(ownerUrl , "chooseBuildFrm", "选择房间",500,800);
	}

	/**
	*  验证房号不能为空、必须为已存在的房号、
	*  @param {String} houseNum  房号
	*  @param {Function} callBack  如果房号正确，则调用该回调方法处理相关动作
	*/
	this.validateHouseNum = function(houseNum,callBack){

		if(houseNum == null || houseNum == "")
			return;

		if(callBack == null || typeof callBack != 'function')
			throw "callBack 不能为null 或者 callBack必须为方法";
		if(oldHouseNum == houseNum) return;
		oldHouseNum = houseNum;

		var url = PROJECT_NAME_PATH_MYCE + "/customer_ownerAction.do?method=validationHouse";
		var lesseeUrl = PROJECT_NAME_PATH_MYCE + "/customer_lesseeAction.do?method=validationHouse";
		url = tenementFlag =="lessee" ?lesseeUrl : url;
		var paramet = "houseNumber=" + houseNum;
		sendAjaxRequest(url,paramet,function(result){
										var obj = result.responseText;
										if(obj == "notExist"){
											Alert("该房号不存在");
											houseNumberObj.value = "";
											oldHouseNum = null;
										} else if(obj == "existInList"){
											Alert("该房号已经存在住房信息列表中");
											houseNumberObj.value = "";
											oldHouseNum = null;
										} else if(obj == "true") {
											callBack();
										}else{
											Alert(obj);
											houseNumberObj.value = "";
											oldHouseNum = null;
										}
								}
		);
	}
}

/***************公共方法,类*******************/

/**
* 设置下拉框选中的值
*/
function setSelectValue(selectObj,selectedValue){
	var options = selectObj.options;
	for(i = 0; i < options.length; i++){
		if(options[i].value == selectedValue){
			selectObj.selectedIndex = i;
			return;
		}
	}
}
/**
*  设置性别选中
*/
function setSexValue(sexObj,showValue){
	for(i = 0; i < sexObj.length; i++){
		if(sexObj[i].value == showValue){
			sexObj[i].checked=true;
			return;
		}
	}
}
/**
 * 窗体操作对象
 * @param {Object} saveAndUpdateId  保存并新增按钮ID
 * @param {Object} saveButtonId  保存按钮ID
 * @param {Object} cancelButtonId  取消按钮ID
 */
function WindowObj(saveAndUpdateId,saveButtonId,cancelButtonId){

	//相关按钮
	saveAndNewButton = document.getElementById(saveAndUpdateId);
	saveButton = document.getElementById(saveButtonId);
	cancelButton = document.getElementById(cancelButtonId);

	/**
	 * 设置按钮是否可用
	 * @param {Object} flag  true:可用  false:不可用
	 */
	this.setButtonUserable = function(flag){
		if(saveAndNewButton)
			saveAndNewButton.disabled = flag;
		if(saveButton)
			saveButton.disabled = flag;
		if(cancelButton)
			cancelButton.disabled = flag;
	}

}

/**
*  兴趣爱好操作对象
*/
function Interest(rightSelectId,leftSelectId){

	var rightSelectObj = document.getElementById(rightSelectId);
	var leftSelectObj = document.getElementById(leftSelectId);

	//右移操作
	this.moveToRight = function(thiz){
		var opt = leftSelectObj.options[leftSelectObj.selectedIndex];
	    rightSelectObj.appendChild(opt);
	    setInterest();
	}
	//左移操作
	this.moveToLeft = function(){
 		if (rightSelectObj.selectedIndex<0) return;
        var opt = rightSelectObj.options[rightSelectObj.selectedIndex];
        leftSelectObj.appendChild(opt);
        setInterest();
	}

	//设置选择的兴趣爱好
	function setInterest(){
		var objOptions = rightSelectObj.options;
		var selectVar = "";
		for(i = 0; i < objOptions.length; i++){
			selectVar += objOptions[i].value + ",";
		}
		//selectVar += objOptions[objOptions.length-1].value;

		document.getElementById('interest').value = selectVar;
	}
}

/**
 * 发送ajax请求，简单封装下
 * @param {Object} url  请求URL
 * @param {Object} parameters 请求参数
 * @param {Object} callBack  响应回调
 */
function sendAjaxRequest(url,parameters,callBack){
	 new Ajax.Request(url,
            {method: 'post', parameters: parameters, onComplete: callBack}
            );
}

function setTip()
{
	if(tenementFlag == "lessee")
		tipName = "租户";
}